﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bookshop
{
    public partial class User : Form
    {
        public User()
        {
            InitializeComponent();
            populate();
        }

        SqlConnection Con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C#\MyProject\BookShopDb.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False;");

        private void User_Load(object sender, EventArgs e)
        {
            foreach (Control c in panel3.Controls)
            {
                c.MouseClick += (s, e2) => panel3_MouseClick(panel3, e2);
            }
            foreach (Control c in panel4.Controls)
            {
                c.MouseClick += (s, e2) => panel4_MouseClick(panel4, e2);
            }
            foreach (Control c in panel5.Controls)
            {
                c.MouseClick += (s, e2) => panel5_MouseClick(panel5, e2);
            }
        }

        private void populate()
        {
            Con.Open();
            string sql = "select * from UserTb1";
            SqlDataAdapter sda = new SqlDataAdapter(sql,Con);
            SqlCommandBuilder builder = new SqlCommandBuilder(sda);
            var ds=new DataSet();
            sda.Fill(ds);
            UserDGV.DataSource = ds.Tables[0];
            Con.Close();
        }

        private void label9_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            if (UNameIp.Text == "" || UPhoneIp.Text == "" || UAddIp.Text == "" || UPasswordIp.Text == "")
            {
                MessageBox.Show("请输入完整信息！");
            }
            else
            {
                try
                {
                    Con.Open();
                    string query = "insert into UserTb1 values('" + UNameIp.Text + "','" + UPhoneIp.Text + "','" + UAddIp.Text + "','" + UPasswordIp.Text + "')";
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.ExecuteNonQuery();  //更新数据库,即执行查询命令 
                    MessageBox.Show("成功添加用户账号！");
                    Con.Close();

                    Reset(); //重置
                    populate(); //重新列表
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("添加用户账号失败" + Ex.Message);
                }
                finally
                {
                    if (Con.State == ConnectionState.Open)
                    {
                        Con.Close();
                    }
                }
            }
        }

        private void Reset()
        {
            UNameIp.Text = "";
            UPhoneIp.Text = "";
            UAddIp.Text = "";
            UPasswordIp.Text = "";
        }

        int key = 0;
        private void UserDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            UNameIp.Text = UserDGV.SelectedRows[0].Cells[1].Value.ToString();
            UPhoneIp.Text = UserDGV.SelectedRows[0].Cells[2].Value.ToString();
            UAddIp.Text = UserDGV.SelectedRows[0].Cells[3].Value.ToString();
            UPasswordIp.Text = UserDGV.SelectedRows[0].Cells[4].Value.ToString();

            if (UNameIp.Text == "")
            {
                key = 0;
            }
            else
            {
                key = Convert.ToInt32( UserDGV.SelectedRows[0].Cells[0].Value.ToString() );
            }
        }

        private void ResetBtn_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void DelBtn_Click(object sender, EventArgs e)
        {
            if (key ==0)
            {
                MessageBox.Show("请选择需要删除的用户账号！");
            }
            else
            {
                try
                {
                    Con.Open();
                    string query = "delete from UserTb1 where UId='" + key + "'";
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.ExecuteNonQuery();  //更新数据库,即执行查询命令 
                    MessageBox.Show("成功删除该用户账号！");
                    Con.Close();

                    Reset(); //重置
                    populate(); //重新列表
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("删除该用户账号失败！" + Ex.Message);
                }
                finally
                {
                    if (Con.State == ConnectionState.Open)
                    {
                        Con.Close();
                    }
                }
            }
        }

        private void EditBtn_Click(object sender, EventArgs e)
        {
            if (UNameIp.Text == "" || UPhoneIp.Text == "" || UAddIp.Text == "" || UPasswordIp.Text == "")
            {
                MessageBox.Show("请输入完整信息！");
            }
            else
            {
                try
                {
                    Con.Open();
                    string query = "update UserTb1 set UName='" + UNameIp.Text + "',UPhone='" + UPhoneIp.Text + "',UAdd='" + UAddIp.Text + "',UPassword='" + UPasswordIp.Text + "' where UId='"+key+"'";
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.ExecuteNonQuery();  //更新数据库,即执行查询命令 
                    MessageBox.Show("编辑用户信息成功！");
                    Con.Close();

                    Reset(); //重置
                    populate(); //重新列表
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("编辑用户信息失败" + Ex.Message);
                }
                finally
                {
                    if (Con.State == ConnectionState.Open)
                    {
                        Con.Close();
                    }
                }
            }
        }

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            if (SearchIp.Text=="")
            {
                MessageBox.Show("请输入用户名！");
            }
            else
            {
                try
                {
                    Con.Open();
                    string query = "select * from UserTb1 where UName like '"+SearchIp.Text+"%'";
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.ExecuteNonQuery();  //更新数据库,即执行查询命令 
                    Con.Close();

                    SqlDataAdapter sda = new SqlDataAdapter(query, Con);
                    SqlCommandBuilder builder = new SqlCommandBuilder(sda);
                    var ds = new DataSet();
                    sda.Fill(ds);
                    UserDGV.DataSource = ds.Tables[0];
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("搜索失败" + Ex.Message);
                }
                finally
                {
                    if (Con.State == ConnectionState.Open)
                    {
                        Con.Close();
                    }
                }
            }
        }

        private void panel3_MouseClick(object sender, MouseEventArgs e)
        {
            this.Hide();
            Books obj = new Books();
            obj.Show();
        }

        private void panel4_MouseClick(object sender, MouseEventArgs e)
        {
            this.Hide();
            DashBoard obj = new DashBoard();
            obj.Show();
        }

        private void panel5_MouseClick(object sender, MouseEventArgs e)
        {
            this.Hide();
            login obj = new login();
            obj.Show();
        }

        private void SearchIp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true; // 阻止默认的回车行为
                SearchBtn_Click(sender,e);
            }
        }
    }
}
